package com.study.opensource.spring.condition.config;


import com.study.opensource.spring.condition.annotation.ConditionalOnSystemProperty;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.MultiValueMap;

/**
 * @author 许棒棒
 * @className com.study.study.opensource.spring.condition.config.OnSystemPropertyCondition
 * @description TODO 类描述
 * @date 2021/04/17 23:31:26
 */
public class OnSystemPropertyCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {

        MultiValueMap<String, Object> allAnnotationAttributes =
                annotatedTypeMetadata.getAllAnnotationAttributes(ConditionalOnSystemProperty.class.getName());
        String propertyName = (String) allAnnotationAttributes.getFirst("name");
        String propertyValue = (String) allAnnotationAttributes.getFirst("value");

        String systemPropertyValue = System.getProperty(propertyName);


        if (propertyValue.equals(systemPropertyValue)) {
            System.out.printf("系统属性找到匹配 : %s %s", propertyName, propertyValue);
            return true;
        }

        return false;
    }
}
